#include "strategies/SimpleDrawingStrategy.h"

bool SimpleDrawingStrategy::pickFromDiscard (CCardStack* hand, const GameState* gameState) {
    bool decidePick;
    const CCard& topOfDiscard = gameState->getCardsInDiscard().back();
    // Make decision based on gain from discard
    int discardValue = calculateGain(hand, CCard(topOfDiscard.GetIndex()), true, gameState);
    Query::debugOut(string("Discard value is "), 2);
    Query::debugValue(discardValue, 2);
    if (discardValue > 0) {
        decidePick = (discardValue >= valueThreshold);
        // Calculate decision certainty
        int numSmaller = 0;
        CCardStack possibleCards = gameState->getUnknownCards(gameState->getCurrPlayer());
        if (possibleCards.Size() > 0) {
            for (CONST_VI ci = possibleCards.begin(); ci != possibleCards.end(); ci++) {
                int gain = calculateGain(hand, CCard(ci->GetIndex()), false, gameState);
                if (gain <= discardValue) {
                    numSmaller++;
                }
            }
            if (decidePick) {
                decisionCertainty = (100*numSmaller) / possibleCards.Size();
            }
            else {
                decisionCertainty = (100*(possibleCards.Size() - numSmaller)) / possibleCards.Size();
            }
        }
        else {
            decidePick = true;
            decisionCertainty = 100;
        }
    }
    else {
        decidePick = false;
        decisionCertainty = 100;
    }
    return decidePick;
}

int SimpleDrawingStrategy::calculateGain(CCardStack* hand, const CCard& card, bool picked, const GameState* gameState) {
    int ret = 0;
    if (discardStrategy != NULL) {
        hand->Push(card);
        int discardDecision = discardStrategy->pickDiscard(hand, picked, gameState);
        hand->Pop();
        ret = Query::gainFromCard(*hand, card, discardDecision);
    }
    else {
        Query::debugOut(string("Discard strategy not set for EVDS"), 1);
    }
    return ret;
}

//int forcedDiscard, discardValue;
//    if (discardStrategy != NULL) {
//        hand->Push(CCard(topOfDiscard.GetIndex()));
//        forcedDiscard = discardStrategy->pickDiscard(hand, true, gameState);
//        hand->Pop();
//        discardValue = Query::gainFromCard(*hand, card, forcedDiscard);
//    }
//    else {
//        decisionCertainty = 0;
//        Query::debugOut(string("Discard strategy not set for FDDS"), 1);
//        return false;
//    }
//    if (discardValue > 0) {
//        int drawValue = 0;
//        int drawCount = 0;
//        CCardStack possibleCards = gameState->getUnknownCards(gameState->getCurrPlayer());
//        int otherPlayer = (gameState->getCurrPlayer() + 1) % 2;
//        CCardStack oppHand = gameState->getCardsPickedUpAndHeldByPlayer(otherPlayer);
//        for (CONST_VI ci = possibleCards.begin(); ci != possibleCards.end(); ci++) {
//            int toDiscard = discardStrategy->pickDiscard(*hand, false, gameState);
//            if (toDiscard != forcedDiscard)
//        }
//    }
//    else {
//        decidePick = false;
//        decisionCertainty = 100;
//    }
//    
